<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card v-loading="loading">
        <!-- 栅格布局 搜索新增开始 -->
        <div class="app-containers">
          <el-row :gutter="20">
            <el-col :span="5">
              <div class="grid-content bg-purple">
                <el-input v-model="subjectName" placeholder="请输入学科内容" />
              </div>
            </el-col>
            <el-col :span="16">
              <div class="grid-content bg-purple">
                <el-button @click="clearFn()">清除</el-button>
                <el-button type="primary" @click="searchFn()">搜索</el-button>
              </div>
            </el-col>
            <el-col :span="1">
              <div class="grid-content bg-purple">
                <el-button type="success" @click="addFn()">新增学科</el-button>
              </div>
            </el-col>
          </el-row>
        </div>
        <!-- 栅格布局 搜索新增结束 -->
        <el-alert
          :title="`数据一共${counts}条`"
          type="info"
          show-icon
          :closable="false"
        />
        <!-- 渲染数据 -->
        <el-table :data="tableData" style="width: 100%">
          <el-table-column
            label="序号"
            type="index"
            :index="indexMethod"
            sortable=""
          />
          <el-table-column prop="subjectName" label="学科名称" width="80px" />
          <el-table-column prop="username" label="创建者" width="100px" />
          <el-table-column prop="addDate" label="创建日期">
            <template #default="{ row }">
              {{ row.addDate | formatTime('YYYY-MM-DD HH:MM:ss') }}
            </template>
          </el-table-column>
          <el-table-column
            prop="isFrontDisplay"
            label="前台是否显示"
            width="120px"
          />
          <el-table-column
            prop="twoLevelDirectory"
            label="二级目录"
            width="50px"
          />
          <el-table-column prop="tags" label="标签" width="80px" />
          <el-table-column prop="totals" label="题目数量" width="80px" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                type="text"
                size="small"
                @click="
                  $router.push({
                    path: `/subjects/directorys/`,
                    query: { id: row.id }
                  })
                "
              >
                学科分类
              </el-button>
              <el-button
                type="text"
                size="small"
                @click="
                  $router.push({
                    path: `/subjects/label/`,
                    query: { id: row.id }
                  })
                "
              >
                学科标签
              </el-button>
              <el-button type="text" size="small" @click="editFn(row.id)">
                修改
              </el-button>
              <el-button type="text" size="small" @click="delFn(row.id)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件开始 -->
        <el-row>
          <el-col :span="12"><div class="grid-content bg-purple" /></el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple-light">
              <div class="fycs">
                <el-pagination
                  :current-page="currentPage4"
                  :page-sizes="[5, 10, 15, 20]"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="counts"
                  :page-count="counts * 0"
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                />
              </div>
            </div>
          </el-col>
        </el-row>

        <!-- 分页组件结束 -->
      </el-card>
      <!-- 新增编辑的弹层 -->
      <el-dialog
        :visible="showDialog"
        :title="showDialogTitle"
        @close="closeFn()"
      >
        <!-- 表单内容 -->
        <el-form label-width="100px">
          <el-form-item label="学科名称">
            <el-input v-model="formData.subjectName" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.isFrontDisplay"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right">
            <el-button @click="closeFn()">取消</el-button>
            <el-button type="primary" @click="submitsubject()">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getsubjectslistApi,
  getDelsubjectApi,
  getAddsubjectApi,
  geteditsubjectApi,
  getLookubjectApi,
  getsearchlistApi
} from '@/api/subjeckts'
export default {
  data() {
    return {
      // showDialogTitle: '新增学科',
      formData: {
        subjectName: '', // 学科名字
        isFrontDisplay: '0'
      },
      showDialog: false, // 弹层
      loading: false,
      counts: 0,
      page: 1,
      pagesize: 10,
      subjectName: '', // 学科名字
      currentPage4: 1, // 分页组件
      tableData: [] // 表格假数据
    }
  },
  computed: {
    // 通过计算属性来计算 是编辑还是添加 的标题 通过id来判断
    showDialogTitle() {
      return this.formData.id ? '编辑学科' : '新增学科'
    }
  },
  created() {
    this.getsubjectslist() // 获取当前页数据
  },
  methods: {
    // clearF 清除
    clearFn() {
      this.subjectName = ''
    },
    // 搜索
    async searchFn() {
      this.loading = true // 设置加载等待动画
      const data = await getsearchlistApi(this.subjectName)
      this.tableData = data.items
      this.counts = data.counts
      if (this.counts === 0) {
        this.$message.error('没有当前学科')
      }
      this.loading = false
    },
    // 获取当前页数据
    async getsubjectslist() {
      this.loading = true // 设置加载等待动画
      const res = await getsubjectslistApi(this.page, this.pagesize)
      this.counts = res.counts
      this.tableData = res.items
      this.loading = false
    },
    // 页码数据改变时
    handleSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.getsubjectslist() // 数据发生改变重新渲染数据
    },
    // 页面变动的数据
    handleCurrentChange(val) {
      this.page = val
      this.getsubjectslist() // 数据发生改变重新渲染数据
    },
    // 处理序号问题 根据index的变化 进
    indexMethod(index) {
      // 当前返回的序号 前面有多少条数据
      // 前面一共有多少条数据 前面的多少页 * 每页的数据的条数 + index +1
      return (this.page - 1) * this.pagesize + index + 1
    }, // 删除数据
    delFn(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await getDelsubjectApi(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }, // 关闭弹层
    closeFn() {
      this.showDialog = false
    }, // 修改学科打开窗口
    async editFn(id) {
      const data = await getLookubjectApi(id) // 获取数据回显
      this.formData = data
      this.showDialog = true

      console.log(id)
    }, // 新增学科打开窗口
    addFn() {
      this.showDialog = true
    },
    // 新增学科请求接口
    async submitsubject() {
      if (!this.formData.id) {
        await getAddsubjectApi(this.formData)
        this.$message.success('添加成功')
      } else {
        this.showDialog = false
        await geteditsubjectApi(this.formData)
        this.$message.success('编辑成功')
      }
      this.formData = {} // 提交完了重置一下表单
      this.getsubjectslist() // 提交完了重新请求数据
      this.showDialog = false
    }
  }
}
</script>
<style lang="scss" scoped>
.app-containers {
  margin-bottom: 15px;
}
::v-deep .el-table thead {
  color: #909399;
  font-weight: 500;
  background-color: #909399;
}
.fycs {
  margin-top: 15px;
}
::v-deep.el-dialog__header {
  background: -webkit-gradient(
    linear,
    left top,
    right top,
    from(#1493fa),
    to(#01c6fa)
  );
  background: linear-gradient(90deg, #1493fa, #01c6fa);
  background: #409eff;
  border-radius: 9px 9px 0 0;
}
</style>
